package jp.ac.u_tokyo.dch.view.outputschema;

import java.util.ArrayList;
import java.util.List;

import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.core.exceptions.DBException;
import jp.ac.u_tokyo.dch.dao.common.FieldMasterDao;
import jp.ac.u_tokyo.dch.model.db.FieldMaster;
import jp.ac.u_tokyo.dch.view.Constants;

import org.apache.commons.lang.xwork.StringUtils;

public class CreateInputItemView {

	private String selectField;

	private String selectFieldName;

	private String editIndex;
	private String deleteIndex;
	private String type;
	private String optionKbn;
	private List<String> options;
	private String optionResult;

	public CreateInputItemView() {
		options = new ArrayList<String>();

	}

	public String getSelectField() {
		return selectField;
	}
	public void setSelectField(String selectField) {
		this.selectField = selectField;
	}


	public String getSelectFieldName() {
		return selectFieldName;
	}

	public void setSelectFieldName(String selectFieldName) {
		this.selectFieldName = selectFieldName;
	}

	public String getEditIndex() {
		return editIndex;
	}

	public void setEditIndex(String editIndex) {
		this.editIndex = editIndex;
	}

	public String getDeleteIndex() {
		return deleteIndex;
	}

	public void setDeleteIndex(String deleteIndex) {
		this.deleteIndex = deleteIndex;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getOptionKbn() {
		return optionKbn;
	}

	public void setOptionKbn(String optionKbn) {
		this.optionKbn = optionKbn;
	}

	public List<String> getOptions() {
		return options;
	}

	public void setOptions(List<String> options) {
		this.options = options;
	}

	public void addRow() {
		options.add("");
	}

	public void deleteRow() {

		Integer rowIndex = Integer.valueOf(deleteIndex);
		options.remove(rowIndex.intValue());
	}

	public void viewInit(DBConnection dbConnection) throws DBException {

		if (StringUtils.isEmpty(type)) {
			type = "0";
		}

		if (StringUtils.isEmpty(optionKbn)) {

			optionKbn = "off";
		} else {

			optionKbn = (optionKbn.equals("0") ? "off" : "on");
		}

		if ("7".equals(type)) {

			FieldMasterDao dao = new FieldMasterDao(dbConnection);

			FieldMaster fieldMaster = dao.selectObject(optionResult);
			selectFieldName = fieldMaster.getDisplayName();
		}

		if (!StringUtils.isEmpty(optionResult)) {
			for (String option : optionResult.split(Constants.OPTION_DEL)) {
				options.add(option);
			}
		}
	}

	public boolean isOptionsEmpty() {

		if (options.size() == 0) {
			return true;
		}

		for (String option : options) {

			if (StringUtils.isEmpty(option.replaceAll("\"", ""))) {
				return true;

			}
		}

		return false;
	}

	public void makeResult() {

		StringBuilder sb = new StringBuilder();

		for (String option : options) {
			sb.append(option.replaceAll("\"", ""));
			sb.append(Constants.OPTION_DEL);
		}
		sb.deleteCharAt(sb.length() - 1);

		optionResult = sb.toString();

		optionKbn = ("on".equals(optionKbn) ? "1" : "0");
	}

	public String getOptionResult() {
		return optionResult;
	}

	public void setOptionResult(String optionResult) {
		this.optionResult = optionResult;
	}

	public void setSelectFieldView(DBConnection dbConnection) throws DBException {


		FieldMasterDao dao = new FieldMasterDao(dbConnection);

		FieldMaster fieldMaster = dao.selectObject(selectField);

		options = new ArrayList<String>();

		options.add(selectField);

		selectFieldName = fieldMaster.getDisplayName();
	}

}
